const path = require('path');
const { VueLoaderPlugin } = require('vue-loader');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');

module.exports = {
  entry: './src/renderer.js',
  output: {
    path: path.resolve(__dirname, '.webpack/renderer'),
    publicPath: './',
    filename: 'renderer.js',
  },
  resolve: {
    extensions: ['.js', '.json', '.node', '.vue'],
    alias: {
      '@': path.resolve(__dirname, 'src'),
      'three': path.resolve(__dirname, 'node_modules/three'),
      'three/examples/jsm/controls/OrbitControls': path.resolve(__dirname, 'node_modules/three/examples/jsm/controls/OrbitControls.js'),
      'three/examples/jsm/loaders/PLYLoader': path.resolve(__dirname, 'node_modules/three/examples/jsm/loaders/PLYLoader.js')
    }
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      // 添加其他你需要的规则
      {
        test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|eot|ttf|otf)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]',
          outputPath: 'assets/',
          publicPath: './assets/'
        }
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          }
        }
      }
    ]
  },
  plugins: [
    new VueLoaderPlugin(),
    new HtmlWebpackPlugin({
      template: './public/index.html',  // 确保模板路径正确
      filename: 'index.html',
      inject: true
    }),
    new webpack.DefinePlugin({
      'process.env.ASSET_PATH': JSON.stringify(process.env.NODE_ENV === 'production' 
        ? '.webpack/x64/renderer/assets/'
        : '/src/assets/')
    }),
    new CopyWebpackPlugin({
      patterns: [
        { from: 'public/textures', to: 'textures' }, // 确保将 textures 文件夹复制到输出目录
        { from: 'public/models', to: 'models' },
        { from: 'src/assets', to: 'assets' }
      ]
    }),
    new webpack.DefinePlugin({
      'process.env.VUE_APP_ASSET_PATH': JSON.stringify(process.env.VUE_APP_ASSET_PATH)
    }),
  ]
  
};